﻿using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

namespace WzhAddon.ExtMP3
{
	public class MP3Player
	{
		public static AudioSource audioSrc
		{
			get
			{
				if (_audioSrc == null)
				{
					_audioSrc = BaseMgr<HSVMDAnimationMgr>.Instance.gameObject.AddComponent<AudioSource>();
				}
				return _audioSrc;
			}
		}

		[DllImport("winmm.dll")]
		private static extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);
		[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
		private static extern int GetShortPathName(string longPath, string shortFile, int bufSize);

		public static int audioType = -1;
		public static string lastAudioFile;
		private static AudioSource _audioSrc;
		public static bool isDelay;
		public static float delayTime;
		private static HSVMDAnimationController vmdCotroller;
		private static int isPlayed = 0;
		public static void Open(string file)
		{
			string text;
			if (audioType == 0)
			{
				text = "stop Music";
				mciSendString(text, null, 0, 0);
				text = "close Music";
				mciSendString(text, null, 0, 0);
			}
			lastAudioFile = file;
			string text2 = file.Substring(file.LastIndexOf(".") + 1, file.Length - file.LastIndexOf(".") - 1);
			if (text2.ToLower().Equals("ogg") || text2.ToLower().Equals("wav"))
			{
				audioType = 1;
				isPlayed = 0;
				BaseMgr<HSVMDAnimationMgr>.Instance.LoadAudio(file, 1);
				return;
			}
			audioType = 0;
			string text3 = "";
			text3 = text3.PadLeft(260, Convert.ToChar(" "));
			if (GetShortPathName(file, text3, text3.Length) <= 0)
			{
				throw new Win32Exception();
			}
			text3 = text3.Trim();
			text3 = text3.Substring(0, text3.Length - 1);
			text = "open \"" + text3 + "\" type MPEGVideo alias Music";
			Console.WriteLine("command: " + text);
			int num = mciSendString(text, null, 0, 0);
			Console.WriteLine("command: " + num);
		}

		public static void SetDelayEnabled(bool isD, HSVMDAnimationController vac)
		{
			if (isD != isDelay)
			{
				isDelay = isD;
				vmdCotroller = vac;
				if (isDelay)
				{
					Seek(vmdCotroller.GetAnimTime());
				}
			}
		}
		public static void SetDelayTime(float dTime)
		{
			if (isDelay)
			{
				delayTime = dTime;
				Seek(vmdCotroller.GetAnimTime());
			}
		}
		public static void Play(HSVMDAnimationController vc = null)
		{
			if (audioType == 0)
			{
				if (!isDelay)
				{
					mciSendString("play Music", null, 0, 0);
					return;
				}
				if (vc != null && vmdCotroller != vc)
				{
					vmdCotroller = vc;
				}
				isPlayed = 1;
				if (vmdCotroller != null)
				{
					Seek(vmdCotroller.GetAnimTime());
					return;
				}
			}
			else if (audioType == 1 && _audioSrc != null)
			{
				if (isDelay)
				{
					_audioSrc.Stop();
					float num = vmdCotroller.GetAnimTime() - delayTime;
					if (num >= 0f)
					{
						_audioSrc.time = num;
						_audioSrc.Play();
						return;
					}
					if (num < 0f)
					{
						float num2 = Mathf.Abs(num);
						_audioSrc.time = 0f;
						_audioSrc.PlayDelayed(num2);
						return;
					}
				}
				else
				{
					_audioSrc.Play();
				}
			}
		}
		public static void Pause()
		{
			if (audioType == 0)
			{
				isPlayed = 0;
				mciSendString("pause Music", null, 0, 0);
				return;
			}
			if (audioType == 1)
			{
				_audioSrc.Pause();
			}
		}
		private static void Seek(float time)
		{
			float num = time - delayTime;
			if (audioType == 0)
			{
				if (num < 0f)
				{
					mciSendString("stop Music", null, 0, 0);
					return;
				}
				int num2 = (int)(num * 1000f);
				mciSendString("seek Music to " + num2, null, 0, 0);
				if (isPlayed > 0)
				{
					mciSendString("play Music", null, 0, 0);
					isPlayed = 2;
					return;
				}
			}
			else if (audioType == 1 && _audioSrc != null)
			{
				if (num >= 0f)
				{
					_audioSrc.time = num;
					return;
				}
				if (num < 0f)
				{
					_audioSrc.Stop();
					float num3 = Mathf.Abs(num);
					_audioSrc.time = 0f;
					_audioSrc.PlayDelayed(num3);
				}
			}
		}

		public static void Stop()
		{
			if (audioType == 0)
			{
				isPlayed = 0;
				mciSendString("stop Music", null, 0, 0);
				return;
			}
			if (audioType == 1 && _audioSrc != null)
			{
				_audioSrc.Stop();
			}
		}

		public static void Update()
		{
			if (audioType == 0 && isPlayed == 1 && isDelay)
			{
				Play(null);
			}
		}
	
	}
}
